﻿#include "ordercntdlg.h"
#include "ui_ordercntdlg.h"
#include"ufxconfig.h"

OrderCntDlg::OrderCntDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::OrderCntDlg)
{
    ui->setupUi(this);

    ui->m_DefaultCnt->setText("1");

    connect(ui->radioBuChuLi,SIGNAL(toggled(bool)),this,SLOT(EnableAlt(bool)));

    ui->radioAll->setChecked(true);
    ui->checkBoxAlt->setEnabled(ui->radioBuChuLi->isChecked());
    ui->m_PingjincangCheckbox->setChecked(true);
    ui->m_ClosePositioncheckBox->setChecked(true);

    ui->m_PricecomboBox->insertItem(0,QStringLiteral("对手价"));
    ui->m_PricecomboBox->insertItem(1,QStringLiteral("市场价"));
    ui->m_PricecomboBox->insertItem(2,QStringLiteral("超价"));

    ui->m_CountComboBox->insertItem(0,QStringLiteral("默认手数平仓"));
    ui->m_CountComboBox->insertItem(1,QStringLiteral("持仓全平"));
    ui->m_CountComboBox->setCurrentIndex(1);

    connect(ui->m_DefaultCnt,SIGNAL(textChanged(QString)),this,SLOT(DefaultCntChanged(QString)));

    //从配置文件中获取默认下单手数
    QString cnt = UFXConfig::getInstance()->GetString("Default","Count","");
    ui->m_DefaultCnt->setText(cnt);

    QString PingJinCangFlg = UFXConfig::getInstance()->GetString("OrderSettings","PingJinCangFlg","");
    if(PingJinCangFlg == "1"){
        ui->m_PingjincangCheckbox->setChecked(true);
    }else{
        ui->m_PingjincangCheckbox->setChecked(false);
    }
    int HoldsType = UFXConfig::getInstance()->GetString("OrderSettings","HoldsType","").toInt();
    switch(HoldsType){
    case 1:
        ui->radioAll->setChecked(true);
        break;
    case 2:
        ui->radioBuChuLi->setChecked(true);
        break;
    case 3:
        ui->radioBuChuLi->setChecked(true);
        ui->checkBoxAlt->setChecked(true);
        break;
    case 4:
        ui->radioFanXiang->setChecked(true);
        break;
    }
    QString PingCangConfirmFlg = UFXConfig::getInstance()->GetString("OrderSettings","PingCangConfirmFlg","");
    if(PingCangConfirmFlg == "1"){
        ui->m_ClosePositioncheckBox->setChecked(true);
    }else{
        ui->m_ClosePositioncheckBox->setChecked(false);
    }
    int PingCangPriceType = UFXConfig::getInstance()->GetString("OrderSettings","PingCangPriceType","").toInt();
    ui->m_PricecomboBox->setCurrentIndex(PingCangPriceType);
    int PingCangCntType = UFXConfig::getInstance()->GetString("OrderSettings","PingCangCntType","").toInt();
    ui->m_CountComboBox->setCurrentIndex(PingCangCntType);
}

OrderCntDlg::~OrderCntDlg()
{
    delete ui;
}

int OrderCntDlg::GetDefaultOrderCnt(){
    return ui->m_DefaultCnt->text().toInt();
}
void OrderCntDlg::SetDefaultOrderCnt(int cnt){
    ui->m_DefaultCnt->setText(QString::number(cnt));
}
void OrderCntDlg::EnableAlt(bool b){
    ui->checkBoxAlt->setEnabled(b);
}
void OrderCntDlg::DefaultCntChanged(const QString &str){
    //更改配置文件中的默认下单手数
    UFXConfig::getInstance()->SetString("Default","Count",str.toStdString().c_str());
}
void OrderCntDlg::OKButtonClicked(){
    if(ui->m_PingjincangCheckbox->checkState() == Qt::Checked){
        UFXConfig::getInstance()->SetString("OrderSettings","PingJinCangFlg","1");
    }else if(ui->m_PingjincangCheckbox->checkState() == Qt::Unchecked){
        UFXConfig::getInstance()->SetString("OrderSettings","PingJinCangFlg","0");
    }

    if(ui->radioAll->isChecked()){
        UFXConfig::getInstance()->SetString("OrderSettings","HoldsType","1");
    }
    if(ui->radioBuChuLi->isChecked()){
        if(ui->checkBoxAlt->checkState() == Qt::Unchecked){
            UFXConfig::getInstance()->SetString("OrderSettings","HoldsType","2");
        }else{
            UFXConfig::getInstance()->SetString("OrderSettings","HoldsType","3");
        }
    }
    if(ui->radioFanXiang->isChecked()){
        UFXConfig::getInstance()->SetString("OrderSettings","HoldsType","4");
    }

    if(ui->m_ClosePositioncheckBox->isChecked()){
        UFXConfig::getInstance()->SetString("OrderSettings","PingCangConfirmFlg","1");
    }else{
        UFXConfig::getInstance()->SetString("OrderSettings","PingCangConfirmFlg","0");
    }

    UFXConfig::getInstance()->SetString("OrderSettings","PingCangPriceType",QString::number(ui->m_PricecomboBox->currentIndex()).toStdString().c_str());
    UFXConfig::getInstance()->SetString("OrderSettings","PingCangCntType",QString::number(ui->m_CountComboBox->currentIndex()).toStdString().c_str());
}
bool OrderCntDlg::GetPingJinCangFlg(){
    return ui->m_PingjincangCheckbox->isChecked();
}

bool OrderCntDlg::GetClosePositionFlg(){
    return ui->m_ClosePositioncheckBox->isChecked();
}
QString  OrderCntDlg::GetClosePositionPriceType(){
    return ui->m_PricecomboBox->currentText();
}
QString  OrderCntDlg::GetClosePositionCntType(){
    return ui->m_CountComboBox->currentText();
}
